package auxiliares;

import business.Panorama;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;


public class EquirectangularToCubic {

    static String format = "jpg";
    // The following can be overriden/set by the indicated command line arguments
    static int overlap = 1;           	  // -overlap
    static String interpolation = "lanczos2";   // -interpolation (lanczos2, bilinear, nearest-neighbor)

    public static ArrayList<BufferedImage> processImageFile(BufferedImage equi) throws IOException {
        
//        Panorama.salvarPano(equi, "equi.png");
        
//        Equi2Rect.init();

        ArrayList<BufferedImage> saida = new ArrayList<BufferedImage>();

        int equiWidth = equi.getWidth();
        int equiHeight = equi.getHeight();

//        if (equiWidth != equiHeight * 2) {
            int equiData[][] = new int[equiHeight][equiWidth];
            new ImageTo2DIntArrayExtractor(equiData, (Image) equi).doit();

            double fov; // horizontal field of view
           
            double r = equiWidth / (2D * Math.PI);
            double y = (Math.tan(Math.PI / 4D) * r + overlap);
            fov = Math.atan(y / r) * 180 / Math.PI * 2;
            int rectWidth = (int) (y * 2);
            int rectHeight = rectWidth;

            Boolean bilinear;
            Boolean lanczos2;
            if (interpolation == "lanczos2") {
                lanczos2 = true;
                bilinear = false;
            } else if (interpolation == "bilinear") {
                lanczos2 = false;
                bilinear = true;
            } else if (interpolation == "nearest-neighbor") {
                lanczos2 = false;
                bilinear = false;
            } else {
                lanczos2 = true;
                bilinear = false;
            }

            int rectData[];
            double pitch;
            double yaw;
            BufferedImage output;

            Equi2Rect.initForIntArray2D(equiData);

            yaw = 0;
            pitch = 0;
            rectData = Equi2Rect.extractRectilinear(yaw, pitch, fov, equiData, rectWidth, rectHeight, equiWidth, bilinear, lanczos2);
            output = new BufferedImage(rectWidth, rectHeight, BufferedImage.TYPE_INT_RGB);
            //setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) 
            output.setRGB(0, 0, rectWidth, rectHeight, rectData, 0, rectWidth);
            saida.add(output);
//            Panorama.salvarPano(output, "1.png");

            yaw = 90;
            pitch = 0;
            rectData = Equi2Rect.extractRectilinear(yaw, pitch, fov, equiData, rectWidth, rectHeight, equiWidth, bilinear, lanczos2);
            output = new BufferedImage(rectWidth, rectHeight, BufferedImage.TYPE_INT_RGB);
            output.setRGB(0, 0, rectWidth, rectHeight, rectData, 0, rectWidth);
            saida.add(output);

            yaw = 180;
            pitch = 0;
            rectData = Equi2Rect.extractRectilinear(yaw, pitch, fov, equiData, rectWidth, rectHeight, equiWidth, bilinear, lanczos2);
            output = new BufferedImage(rectWidth, rectHeight, BufferedImage.TYPE_INT_RGB);
            output.setRGB(0, 0, rectWidth, rectHeight, rectData, 0, rectWidth);
            saida.add(output);

            yaw = 270;
            pitch = 0;
            rectData = Equi2Rect.extractRectilinear(yaw, pitch, fov, equiData, rectWidth, rectHeight, equiWidth, bilinear, lanczos2);
            output = new BufferedImage(rectWidth, rectHeight, BufferedImage.TYPE_INT_RGB);
            output.setRGB(0, 0, rectWidth, rectHeight, rectData, 0, rectWidth);
            saida.add(output);

//        }
        return saida;
    }
}
class FileListFilter implements FilenameFilter {

    private Vector<String> extensions;

    public FileListFilter(Vector<String> extensions) {
        this.extensions = extensions;
    }

    public boolean accept(File directory, String filename) {
        if (extensions != null) {
            Iterator<String> itr = extensions.iterator();
            while (itr.hasNext()) {
                String ext = (String) itr.next();
                if (filename.endsWith('.' + ext)) {
                    return true;
                }
            }
        }
        return false;
    }
}